package team.ttorder.ttorder.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import team.ttorder.ttorder.javabean.PageBean;
import team.ttorder.ttorder.model.Order;
import team.ttorder.ttorder.model.OrderDetail;
import team.ttorder.ttorder.service.IOrderService;
import team.ttorder.ttorder.utils.CommonMessageResult;

@RestController
@RequestMapping("/order")
public class OrderController {
	
	@Autowired
	private IOrderService orderService;
	
	@RequestMapping(value = "/addOrder", method = RequestMethod.POST)
	public CommonMessageResult addOrder(@Valid Order order, BindingResult bindingResult) throws Throwable {
		CommonMessageResult result = new CommonMessageResult();
		Map<String,String> messageMap=new HashMap<String,String>();
		result.setSuccess(false);
		
		try {
			orderService.addOrder(order);
			result.setSuccess(true);
		} catch (Exception e) {
			
			e.printStackTrace();
			messageMap.put("message", e.getMessage());
			result.setMessageMap(messageMap);
		}
		return result;
	}
	
	@RequestMapping(value = "/addOrderDetail", method = RequestMethod.POST)
	public CommonMessageResult addOrderDetail(@Valid OrderDetail orderDetail, BindingResult bindingResult) throws Throwable {
		CommonMessageResult result = new CommonMessageResult();
		Map<String,String> messageMap=new HashMap<String,String>();
		result.setSuccess(false);
		
		try {
			orderService.addOrderDetail(orderDetail);
			result.setSuccess(true);
		} catch (Exception e) {
			
			e.printStackTrace();
			messageMap.put("message", e.getMessage());
			result.setMessageMap(messageMap);
		}
		return result;
	}
	
	@RequestMapping(value = "/getOrderId", method = RequestMethod.POST)
	public Order getOrderId(int tableId ) {
		Order result = orderService.getId(tableId);
		return result;
	}
	
	@RequestMapping(value = "/manageOrders", method = RequestMethod.GET)
	public ModelAndView manageOrder() {
		ModelAndView result = new ModelAndView();
		result.setViewName("/business/order/manageOrder");
		return result;
	}
	
	@RequestMapping(value ="/getOrderById", method = RequestMethod.POST)
	public Order getOrderById(Integer id) {
		Order order = orderService.getOrderById(id);
		if(order != null) {
			if(order.getOrderStatus() == 0) {
				return order;
			}
			return null;
		}else {
			return null;
		}
	}
	
	@RequestMapping(value = "/payComplete", method = RequestMethod.POST)
	public Map<String,Object> payComplete(Integer id){
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("success", false);
		try {
			orderService.payComplete(id);
			map.put("success", true);
		}catch(Exception e) {
			e.printStackTrace();
		}
		return map;
	}
	
	@RequestMapping(value = "/deleteOrder", method = RequestMethod.POST)
	public Map<String,Object> deleteOrder(Integer id){
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("success", false);
		try {
			orderService.deleteOrder(id);
			map.put("success", true);
		}catch(Exception e) {
			e.printStackTrace();
		}
		return map;
	}
	
	@RequestMapping(value = "/getOrderDetail", method = RequestMethod.POST)
	public List<OrderDetail> getOrderDetailByOrderId(Integer id){
		List<OrderDetail> resultList = orderService.getOrderDetailByOrderId(id);
		if(resultList != null) {
			return resultList;
		}
		return null;
	}
	
	@SuppressWarnings("rawtypes")
	@RequestMapping(value = "/findAllOrders", method = RequestMethod.POST)
	public PageBean getOrderList(int pageNum, int pageSize){
		PageBean<Order> pageBean = orderService.findOrders(pageNum, pageSize);
		return pageBean;
	}
}
